const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLoading: true,
    dataType: 1,
    page: 1,
    no_more: false,
    
    shoppingMall:[],
    shoppingMallName:'',
    shoppingMallId:'',

    app_root: '',    
    stage_id:1,//当前期数

    page_sex:2,//页面性别
    page_baoming_text:'女生报名入口',
    sex_color:'baoming_girl',

    // 下拉菜单
    first: '选择城区',
    second: '筛选',
    thirds: '房型',
    fours: '筛选',
    _num: 0,
    _res: 0,
    dist:[],
    dist_id:0,
    shopping_mall_city:'',
    shopping_mall_district:'',
    // 筛选
    sex_res: [{ id: 1, name: '男' }, { id: 2, name: '女' }],
    age_res: [{ id: 1, name: '20岁以下' }, { id: 2, name: '20岁-25岁' }, { id: 3, name: '26岁-30岁' }, { id: 4, name: '31岁-35岁' }, { id: 5, name: '36岁以上' }],
    type_res: [{ id: 1, name: '待撩' }, { id: 2, name: '单身' }, { id: 3,name: '有主' }],
    zhuangxiu: [{ name: '单拍' }, { name: '亲子套餐' }, { name: '活动套餐' }, { name: '女王套餐' }],
   
    one: 0,
    two: 0,
    third: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //let shopping_mall_city = wx.getStorageSync('shopping_mall_city');//城市名称
    let shopping_mall_city = '杭州市';//城市名称
    //let shopping_mall_district = wx.getStorageSync('shopping_mall_district');//区名称
    let shopping_mall_district = '拱墅区';//区名称
    let user_address_id = wx.getStorageSync('user_address_id');//商城id
    let user_address_cont = wx.getStorageSync('user_address_cont');//商城名称
    //缓存中有
    if (shopping_mall_city) {
      this.setData({
        shopping_mall_city: shopping_mall_city
      })
    }
    if (shopping_mall_district) {
      this.setData({
        shopping_mall_district: shopping_mall_district
      })
    }
  
    if (user_address_cont){
      this.setData({
        shoppingMallName: user_address_cont
      })
    }

    this.setData({
      api_root: App.root
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let team = this.data.list;
    //获取 区信息
    this.getDistrict();
    //获取当前期数
    this.getStage();
    // 获取 商城地址
    //this.getShoppingMall();
    // 获取我的团队列表
    if(!team){
      this.getTeamList();
    }
    // 获取当前用户信息
    this.getUserDetail();
  },

  /**
   * 获取当前用户信息
   */
  getUserDetail: function () {
    let _this = this;
    App._get('user.index/detail', {}, function (result) {
      console.log(result.data);
      _this.setData(result.data);
    });
  },

  onReady: function () {
    //页面渲染完成 第三运行
    //console.log('onReady');
  },

  //获取当前期数
  getStage:function(){
    let _this = this;
    App._get('enroll/getStage', {}, function (result) {
      
      _this.setData({
        stage_id: result.data
      });
    });
  },

  //获取区信息
  getDistrict:function(){
    let _this = this;
    //let user_address_id =  this.data.shoppingMallId
    let user_address_id = wx.getStorageSync('user_address_id');//商城id
    let shopping_mall_city = this.data.shopping_mall_city;
    let shopping_mall_district = this.data.shopping_mall_district;
    
    App._get('Shopping_mall/getDistrict', {
      shopping_mall_id: user_address_id, 
      shopping_mall_city: shopping_mall_city,
      shopping_mall_district: shopping_mall_district
    }, function (result) {
      console.log(result);
      _this.setData({
        dist: result.data.list,
        dist_id: result.data.list[0].id
      });
    });
  },
  

  /**
   * 获取列表
   */
  getTeamList: function(isNextPage, page) {
    let _this = this;
    //let shoppingMallId = _this.data.shoppingMallId;
    let sex = this.data.page_sex;
    let dist_id = this.data.dist_id
    App._get('enroll/lists', {
      page: page || 1,
      sex: sex,
      dist_id: dist_id
    }, function(result) {
      //console.log(result);
      // 创建页面数据
      let tang = _this.createData(result.data, isNextPage);
      _this.setData(tang);
    });
  },

  /**
   * 创建页面数据
   */
  createData: function(data, isNextPage) {
    data['isLoading'] = false;
    // 列表数据
    let dataList = this.data.list;
    if (isNextPage == true && (typeof dataList !== 'undefined')) {
      data.list.data = dataList.data.concat(data.list.data);
    }
    // 设置swiper的高度
    this.setSwiperHeight();
    return data;
  },

  /**
   * 下拉到底加载数据
   */
  triggerDownLoad: function() {
    // 已经是最后一页
    if (this.data.page >= this.data.list.last_page) {
      this.setData({
        no_more: true
      });
      return false;
    }
    this.getTeamList(true, ++this.data.page);
  },
  //随机刷新
  rand:function(){
    this.setData({
      isLoading: true,
      disdataTypet: 1,
      page: 1,
      no_more: false,
      rand: 2,
      currentTab: 3
    });
    this.getTeamList();
  },
  //刷新页面
  newPage: function () {
    this.setData({
      isLoading: true,
      disdataTypet: 1,
      page: 1,
      no_more: false,
      rand: 1
    });
    this.getTeamList();
  },
  //返回正常排序
  gotoSort:function(){
    this.setData({
      isLoading: true,
      disdataTypet: 1,
      page: 1,
      no_more: false,
      rand: 1
    });
    this.getTeamList();
  },
  /**
   * 设置swiper的高度
   */
  setSwiperHeight: function(isTap) {
    // 获取系统信息(拿到屏幕宽度)
    let systemInfo = wx.getSystemInfoSync(),
      rpx = systemInfo.windowWidth / 750, // 计算rpx
      tapHeight = isTap ? Math.floor(rpx * 82) : 0, // tap高度
      peopleHeight = Math.floor(rpx * 65), // people高度
      tang = Math.floor(rpx * 430), 
      swiperHeight = systemInfo.windowHeight - tapHeight - peopleHeight-tang; // swiper高度
    
    this.setData({
      swiperHeight
    });
  },

  /** 
   * 点击tab切换 
   */
  swichNav: function(e) {
    let _this = this;
    _this.setData({
      dataType: e.target.dataset.current,
      list: {},
      page: 1,
      no_more: false,
      isLoading: true,
    }, function() {
      // 获取我的团队列表
      _this.getTeamList();
    });
  },

  /**
   * 表单提交
   */
  // saveData: function (e) {
  //   let _this = this;
  //   let shoppingMallId = _this.data.shoppingMallId;
  //   App._get('user.dealer.team/lists', {
  //     page: 1,
  //     shoppingMallId: shoppingMallId
  //   }, function (result) {
  //     // 创建页面数据
  //     _this.setData(_this.createData(result.data));
  //   });
  // },

  /**
   * 修改地区
   */
  // bindPickerChange: function (e) {
  //   let index = e.detail.value;
  //   this.setData({
  //     shoppingMallName: this.data.shoppingMall[index]['shopping_mall_cont'],
  //     shoppingMallId: this.data.shoppingMall[index]['shopping_mall_id']
  //   })
  // },


  //下拉js
  isShow: true,
  currentTab: 0,

  // 下拉切换
  hideNav: function () {
    this.setData({
      displays: "none"
    })
  },
  // 区域
  tabNav: function (e) {
    let dis = this.data.displays;
    if (dis == 'block' && this.data.currentTab === e.target.dataset.current){
      this.setData({
        displays: "none"
      })
    }else{
      this.setData({
        displays: "block"
      })
    }
    
    if (this.data.currentTab === e.target.dataset.current) {
      this.setData({
        currentTab: 3,
      })
    } else {
      var showMode = e.target.dataset.current == 0;
      this.setData({
        currentTab: e.target.dataset.current,
        isShow: showMode
      })
    }
  },
  // 下拉切换中的切换
  // 选择 区
  selected: function (e) {
    let _this = this;
    let dist_id = e.currentTarget.dataset.id;
    //let user_address_id = wx.getStorageSync('user_address_id');//商城id
    let user_address_id = this.data.shoppingMallId
    App._get('Shopping_mall/clickDist', {
      dist_id: dist_id,
      shopping_mall_id: user_address_id
    }, function (result) {
      _this.setData({
        dist: result.data.dist_res,
        dist_id: dist_id,
        shoppingMall: result.data.mall,
        displays: "none",
        currentTab: 0
      });

      _this.getTeamList();
    });


  },
  
  // 商城选择
  clickShoppingMall: function (e) {
    let shoppingMall = this.data.shoppingMall;
    for (let i = 0; i < shoppingMall.length; i++) {
      shoppingMall[i]['selected'] = 'add_city';
      if (e.currentTarget.dataset.id == shoppingMall[i]['id']){
        shoppingMall[i]['selected'] = 'add_adress';
      }
    };
    this.setData({
      shoppingMall: shoppingMall,
      shoppingMallId: e.currentTarget.dataset.shopping_mall_id,
      shoppingMallName: e.currentTarget.dataset.name
    });
    this.getTeamList();//显示 社交对象
    this.setData({
      isLoading: true,
      disdataTypet: 1,
      page: 1,
      no_more: false,
      rand: 1,
      displays: "none"
    });
  },

  //显示全部
  allShopping:function(){
    let shoppingMall = this.data.shoppingMall;
    for (let i = 0; i < shoppingMall.length; i++) {
      shoppingMall[i]['selected'] = 'add_city';
    };
    this.setData({
      shoppingMall: shoppingMall,
      shoppingMallId: ''
    });
    this.getTeamList();//显示 社交对象
    this.setData({
      isLoading: true,
      disdataTypet: 1,
      page: 1,
      no_more: false,
      rand: 1,
      displays: "none"
    });
  },

  //关注
  guanzhu:function(e){
    let to_user_id = e.currentTarget.dataset.user_id;
    App._get('user.Follow/follow', {
      to_user_id: to_user_id
    }, function (result) {
      App.showSuccess(result.msg);
    });
  },

  //跳转 用户详情页
  goToDetail:function(e){
    let id = e.currentTarget.dataset.id;
    let page_sex = e.currentTarget.dataset.page_sex;
    wx.navigateTo({
      url: './detail?id=' + id + '&page_sex=' + page_sex
    })
  },

  choseSex: function (e) {
    //console.log(e.currentTarget.dataset.id);
    this.setData({
      sex: e.currentTarget.dataset.id,
      one: e.currentTarget.dataset.id
    })
  },

  choseAge:function(e){
    //console.log(e.currentTarget.dataset.id)
    this.setData({
      age: e.currentTarget.dataset.id,
      two: e.currentTarget.dataset.id
    })
  },

  choseType:function(e){
    //console.log(e.currentTarget.dataset.id)
    this.setData({
      type: e.currentTarget.dataset.id,
      third: e.currentTarget.dataset.id
    })
  },

  //确认 
  queren:function(e){
    let _this = this;
    this.getTeamList();
    this.setData({
      isLoading: true,
      page: 1,
      no_more: false,
      displays: "none"
    })
  },

  //清除
  qingchu:function(){
    this.setData({
      isLoading: true,
      page: 1,
      no_more: false,

      sex:0,
      age:0,
      type:0,
      one: 0,
      two: 0,
      third: 0,
      displays: "none"
    })
    this.getTeamList();
  },

  baoming:function(e){
    let user_id = e.currentTarget.dataset.user_id;
    let page_sex = e.currentTarget.dataset.sex;

    let address_id = this.data.userInfo.address_id;
    let sex = this.data.userInfo.address_default.sex;

    let dist_id = this.data.dist_id; 
    if (!address_id){
      // App.showError('您还未填写基本信息');
      // return false;
      wx.showModal({
        title: '提示',
        confirmText:'去填写',
        content: '您还未填写基本信息',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '../address/create'
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }else{
      //女生报名页面
      if (page_sex == 2){
        if (page_sex == sex) {
          //跳转报名页
          wx.navigateTo({
            url: './enroll?address_id=' + address_id + '&user_id=' + user_id + '&sex=' + sex + '&dist_id=' + dist_id
          })
        } else {
          App.showError('这是女生报名入口,男生请绕道');
          return false;
        }
      }
      //男生报名页面
      if (page_sex == 1){
        if (page_sex == sex) {
          //跳转报名页
          wx.navigateTo({
            url: './enroll?address_id=' + address_id + '&user_id=' + user_id + '&sex=' + sex + '&dist_id=' + dist_id
          })
        } else {
          App.showError('这是男生报名入口,女生请到女生宝贝榜报名');
          return false;
        }
      }
    }
  },

  bindHeaderTap:function(e){
    let page_sex = e.currentTarget.dataset.page_sex;
    if (page_sex == 1){
      this.setData({
        page_baoming_text: '男生报名入口',
        sex_color:'baoming_boy',
      })
    }else{
      this.setData({
        page_baoming_text: '女生报名入口',
        sex_color: 'baoming_girl',
      })
    }
    this.setData({
      page_sex: e.currentTarget.dataset.page_sex,
      isLoading: true,
      page: 1,
      no_more: false,
      displays: "none"
    })

    // 获取我的团队列表
    
      this.getTeamList();
    
  },

  // choseTxtColor: function (e) {
  //   var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
  //   console.log(e.currentTarget.dataset.id)
  //   this.setData({
  //     one: id
  //   })
  // },
  
  // clickMum: function (e) {
  //   this.setData({
  //     _mum: e.target.dataset.num
  //   })
  //   this.setData({
  //     displays: "none"
  //   })
  //   var text = this.data.name
  // },
 
  // clickCum: function (e) {
  //   this.setData({
  //     _cum: e.target.dataset.num
  //   })
  //   this.setData({
  //     displays: "none"
  //   })
  //   var text = this.data.name
  //   console.log(text)
  // },
  
  // 售价
  // clickNum: function (e) {
  //   //console.log(e.target.dataset.num)
  //   this.setData({
  //     _num: e.target.dataset.num
  //   })
  //   this.setData({
  //     second: e.target.dataset.name
  //   })
  //   this.setData({
  //     displays: "none"
  //   })
  //   var text = this.data.name
  //   //console.log(text)
  // },
  
  // 房型
  // clickHouse: function (e) {
  //   this.setData({
  //     _res: e.target.dataset.num
  //   })
  //   this.setData({
  //     thirds: e.target.dataset.name
  //   })
  //   this.setData({
  //     displays: "none"
  //   })
  // },
  

  // 筛选
  
  // chaoxiang: function (e) {
  //   var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
  //   this.setData({
  //     two: id
  //   })
  // },
  // louceng: function (e) {
  //   var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
  //   this.setData({
  //     third: id
  //   })
  // },
  // zhuangxiu: function (e) {
  //   var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
  //   this.setData({
  //     four: id
  //   })
  // },
  // leibei: function (e) {
  //   var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
  //   this.setData({
  //     five: id
  //   })
  // },
  // tese: function (e) {
  //   var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
  //   this.setData({
  //     six: id
  //   })
  // },
  // paixu: function (e) {
  //   var id = e.currentTarget.dataset.id;  //获取自定义的ID值  
  //   this.setData({
  //     seven: id
  //   })
  // }

})